package com.dy.framework.config;

import com.dy.framework.config.mqtt.MqttAcceptClient;
import com.dy.framework.config.mqtt.MqttSendClient;
import com.dy.system.domain.FreshMedicalMachine;
import com.dy.system.service.IFreshMedicalMachineService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class ApplicationRunnerConfig implements ApplicationRunner {

    protected final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private IFreshMedicalMachineService freshMedicalMachineService;
    @Autowired
    private MqttSendClient MqttSendClient;
    @Autowired
    private MqttAcceptClient mqttAcceptClient;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        logger.info("本条代码将在Started MyApplication之后执行");
        List<FreshMedicalMachine> machineList = freshMedicalMachineService.selectFreshMedicalMachineList(new FreshMedicalMachine());
        for (FreshMedicalMachine medicalMachine : machineList) {
            String topicName = medicalMachine.getMachineCode()+"12345678/pub_data";
            logger.info("subscribe topicName ={}",topicName);
            mqttAcceptClient.subscribe(topicName,2);
        }
    }
}
